package com.example.administrator.fastdream.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.administrator.fastdream.R;
import com.example.administrator.fastdream.date.bean.JiangDetailBean;
import com.example.administrator.fastdream.date.presenter.JiangDetailPresenter;
import com.example.administrator.fastdream.view.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JiangDetailActivity extends BaseActivity implements JiangDetailPresenter.JiangDetailCallBack {

    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private JiangDetailPresenter presenter;
    private int page = 1;
    private String issue;
    private RecordAdapter recordAdapter;
    private TextView tvCount;
    private TextView tv_money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jiang_detail);
        ButterKnife.bind(this);
        ivBack.setVisibility(View.VISIBLE);
        issue = getIntent().getStringExtra("issue");
        String time = issue.substring(0, 4) + "年" + issue.substring(4, 6) + "月" + issue.substring(6, 8)
                + "日第" + issue.substring(8, issue.length()) + "期";
        tvTitle.setText(time);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        presenter = new JiangDetailPresenter(this, this);
        presenter.getJiangDetail(issue, page);
        initListener();
    }

    private void initListener() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                presenter.getJiangDetail(issue, page);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                presenter.getJiangDetail(issue, page);
            }
        });
    }

    @Override
    public void getJiangDetail(JiangDetailBean jiangDetailBean) {
        List<JiangDetailBean.DataEntity.DataEntity2> data = jiangDetailBean.getData().getData();
        if (recordAdapter == null) {
            recordAdapter = new RecordAdapter(R.layout.item_zhongjiang_detail, data);
            View view = View.inflate(mContext, R.layout.recycler_header, null);
            ImageView iv_bg = view.findViewById(R.id.iv_bg);
            iv_bg.setBackgroundResource(R.drawable.zhongjiangdetail_bg);
            tvCount = view.findViewById(R.id.tv_count);
            tv_money = view.findViewById(R.id.tv_money);
            tvCount.setText("已抽取" + jiangDetailBean.getData().getLq_money()
                    + "元，还剩" + jiangDetailBean.getData().getSy_money() + "元");
            tv_money.setText("幸运奖金：" + jiangDetailBean.getData().getZong_money());
            recordAdapter.addHeaderView(view);
            recyclerView.setAdapter(recordAdapter);
        } else {
            tvCount.setText("已抽取" + jiangDetailBean.getData().getLq_money()
                    + "元，还剩" + jiangDetailBean.getData().getSy_money() + "元");
            tv_money.setText("幸运奖金：" + jiangDetailBean.getData().getZong_money());
            if (page == 1) {
                recordAdapter.setNewData(data);
                refreshLayout.finishRefresh();
            } else {
                if (data == null || data.size() == 0) {
                    Toast.makeText(this, "到底啦~", Toast.LENGTH_SHORT).show();
                    refreshLayout.finishLoadMore();
                    return;
                }
                recordAdapter.addData(data);
                refreshLayout.finishLoadMore();
            }
        }
    }

    private class RecordAdapter extends BaseQuickAdapter<JiangDetailBean.DataEntity.DataEntity2, BaseViewHolder> {

        public RecordAdapter(int layoutResId, @Nullable List<JiangDetailBean.DataEntity.DataEntity2> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JiangDetailBean.DataEntity.DataEntity2 item) {
            Glide.with(mContext).load(item.getAvatar()).into((ImageView) helper.getView(R.id.iv_head));
            helper.setText(R.id.tv_name, item.getUsername())
                    .setText(R.id.tv_money, item.getMoney() + "元")
                    .setText(R.id.tv_mobile, item.getMobile().substring(0, 3)
                            + "****" + item.getMobile().substring(7, item.getMobile().length()));
        }
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
